package com.kstech.mp3604.fragments

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankii.androidlibrary.base.BaseBindingFragment
import com.kstech.mp3604.adapter.AllTaskAdapter
import com.kstech.mp3604.adapter.GridSpaceItemDecoration
import com.kstech.mp3604.bean.StackCoordinate
import com.kstech.mp3604.bean.TaskData
import com.kstech.mp3604.data.DbManager
import com.kstech.mp3604.databinding.FragmentQueryHistoryTaskBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 查询按钮-历史任务
 */
class QueryHistoryTaskFragment :
    BaseBindingFragment<FragmentQueryHistoryTaskBinding>(FragmentQueryHistoryTaskBinding::inflate) {
    private lateinit var taskAdapter: AllTaskAdapter
    private val dataList = ArrayList<StackCoordinate>()
    override fun onCreateInit() {

    }

    override fun onInitView() {
        lifecycleScope.launch(Dispatchers.IO) {
            val data = DbManager.db.taskDataDao().queryAllTask()
            withContext(Dispatchers.Main) {
                if (data.size > 0) {
                    binding.emptyDataMsg.visibility = View.GONE
                    updateView(data)
                } else {
                    binding.emptyDataMsg.visibility = View.VISIBLE
                    binding.emptyDataMsg.text = "暂无数据"
                }
            }
        }
        binding.queryButton.setOnClickListener {
            val taskIdSearchValue = binding.searchByTaskId.text.toString().trim()
            val batchNoSearchValue = binding.searchBybatchNo.text.toString().trim()
            val timeSearchValue = binding.searchByTime.text.toString().trim()
            lifecycleScope.launch(Dispatchers.IO) {
                var dataList: MutableList<TaskData>? = null
                if (taskIdSearchValue == "" && batchNoSearchValue == "" && timeSearchValue == "") {
                    dataList = DbManager.db.taskDataDao().queryAllTask()

                } else {
                    dataList = DbManager.db.taskDataDao()
                        .queryFindTaskData(taskIdSearchValue, batchNoSearchValue, timeSearchValue)
                }

                withContext(Dispatchers.Main) {
                    binding.emptyDataMsg.visibility = View.GONE
                    updateView(dataList)
                }
            }
        }
        binding.recyclerView.layoutManager = LinearLayoutManager(requireActivity())
        binding.recyclerView.addItemDecoration(GridSpaceItemDecoration(10, 10))
        taskAdapter = AllTaskAdapter(false)
        binding.recyclerView.adapter = taskAdapter
        taskAdapter.submitList(dataList)
    }

    private fun updateView(data: List<TaskData>) {
        dataList.clear()
        //储存格式转换adapter显示格式
        data.forEach {
            it.stackCoordinates!!.forEach { c_it ->
                val s = c_it.apply {
                    taskData = it
                }
                dataList.add(s)
            }
        }
        taskAdapter.notifyDataSetChanged()
    }
}